<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="28">
            <a-col :md="5" :sm="12">
              <a-form-item label="登录名">
                <a-input v-model="queryParam.loginName" placeholder="请输入登录名" style="width: 100%"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="登录地址">
                <a-input v-model="queryParam.ipaddr" placeholder="请输入登录地址" style="width: 100%"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="状态">
                <a-select
                  placeholder="不限"
                  v-model="queryParam.status"
                >
                  <a-select-option value="true">成功</a-select-option>
                  <a-select-option value="false">失败</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="7" :sm="18">
              <a-form-item label="登录时间">
                <a-range-picker v-model="queryParam.loginTime" @change="onChange" style="width: 100%"></a-range-picker>
              </a-form-item>
            </a-col>
            <a-col :md="1" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 30px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
        <a-button v-show="loginLogRemoveEnable && selectedRowKeys.length > 0" type="danger" shape="round" icon="delete" @click="onClick(selectedRowKeys)">删除</a-button>
        <a-button v-show="loginLogRemoveEnable" type="danger" shape="round" icon="delete" @click="removeAll">清空</a-button>
      </div>
      <s-table
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :rowSelection="loginLogRemoveEnable ? {selectedRowKeys: selectedRowKeys, onChange: onSelectChange} : null"
        showPagination="auto"
        :showTotal="true"
      >
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="真的删除吗?" @confirm="delById(record.id)">
            <a v-if="loginLogRemoveEnable"><a-icon type="delete"/>  删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { checkPermission } from '@/utils/permissions'
import { findLoginLogPage, batchDelLoginLog, delAllLoginLog, delOneLoginLog } from '@/api/system'
import { STable } from '@/components'
import message from 'ant-design-vue/es/message'
const statusMap = {
  true: {
    status: 'success',
    text: '正常'
  },
  false: {
    status: 'error',
    text: '停用'
  }
}
export default {
  components: {
    STable
  },
  name: 'LoginLog',
  data () {
    return {
      // 查询参数
      queryParam: {},
      icon: 'eye-invisible',
      hiddenSearch: false,
      loginLogRemoveEnable: checkPermission('log:loginLog:remove'),
      selectedRowKeys: [],
      selectedRows: [],
      // 表头
      columns: [
        {
          title: '登录名',
          dataIndex: 'loginName',
          align: 'center'
        },
        {
          title: 'IP',
          dataIndex: 'ipaddr',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center'
        },
        {
          title: '登录地点',
          dataIndex: 'loginLocation',
          align: 'center'
        },
        {
          title: '浏览器',
          dataIndex: 'browser',
          align: 'center'
        },
        {
          title: '操作系统',
          dataIndex: 'os',
          align: 'center'
        },
        {
          title: '登录时间',
          dataIndex: 'loginTime',
          sorter: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findLoginLogPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    onChange (date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1] + ' 23:59:59'
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    onClick (ids) {
      const self = this
      this.$confirm({
        title: '你确定删除该条登录日志记录吗?',
        content: h => <div style="color:red;">登录日志删除将永久不能恢复</div>,
        onOk () {
          batchDelLoginLog(ids).then(res => {
            self.$refs.table.refresh()
            message.success('登录日志删除成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    },
    removeAll () {
      const self = this
      this.$confirm({
        title: '你确定清空所有登录日志吗?',
        content: h => <div style="color:red;">这项操作,将删除所有登录日志,请谨慎操作</div>,
        onOk () {
          delAllLoginLog().then(res => {
            self.$refs.table.refresh()
            message.success('删除全部登录日志成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    },
    delById (id) {
      delOneLoginLog({ loginLogId: id }).then(res => {
        this.$refs.table.refresh()
        message.success('登录日志删除成功')
      }).catch(res => {
        message.error(((res.response || {}).data || {}).message || res.message)
      })
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
